package server

import (
	"github.com/hyperway/pkg/component"
	"github.com/hyperway/pkg/config"
	"github.com/hyperway/pkg/constant"
	"github.com/hyperway/pkg/logger"
	"github.com/urfave/cli/v2"
)

func RegisterLifecycle(l component.Lifecycle) {
	if p, ok := l.(component.Preparer); ok {
		component.RegisterPrepare(p)
	}
	if d, ok := l.(component.Destroyer); ok {
		component.RegisterDestroy(d)
	}
}

// InitViperComponent 用于初始化Viper组件
func InitViperComponent() cli.ActionFunc {
	return func(context *cli.Context) error {
		if err := config.InitConfig(context.StringSlice(constant.ConfigNames)); err != nil {
			return err
		}
		return nil
	}
}

// InitZapLoggerComponent 用于初始化Zap日志组件
func InitZapLoggerComponent() cli.ActionFunc {
	return func(context *cli.Context) error {
		if err := logger.InitLogger(context.String(constant.LogConfigName)); err != nil {
			return err
		}
		return nil
	}
}

// RunApplication 用于启动应用
func RunApplication() cli.ActionFunc {
	return func(ctx *cli.Context) error {
		if err := Run(); err != nil {
			return err
		}
		return nil
	}
}
